<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\AccountRealname;
use App\Fastapp\Model\FastappRealname;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 用户实名Mapper类
 */
class FastappAccountRealnameMapper extends AbstractMapper
{
    /**
     * @var AccountRealname
     */
    public $model;

    public function assignModel()
    {
        $this->model = AccountRealname::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 主键
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 实名id
        if (isset($params['realname_id']) && filled($params['realname_id'])) {
            $query->where('realname_id', '=', $params['realname_id']);
        }

        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 1全局
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        return $query->with(['realname', 'account:gene_id,mobile,username,email']);
    }

    public function changeStatus(int $id, string $statusValue, string $statusName): bool|int
    {
        return FastappRealname::query()->where(['id' => $id])->update([$statusName => $statusValue]);
    }
}